using System;
using Orm;

namespace ormsamples {
    public class MyPetShop
    {
        private readonly int ROW_COUNT = 100;

		private void CreatePets() {
			PersistentTransaction t = petshop.PetShopPersistentManager.Instance().GetSession().BeginTransaction();
			try {
				petshop.Pet lPet = petshop.PetFactory.CreatePet();
                lPet.Source = "England";
                lPet.Type = "Dog [Shih Tzu]";
                lPet.Save();

                lPet = petshop.PetFactory.CreatePet();
                lPet.Source = "China";
                lPet.Type = "Dog [Golden Retriever]";
                lPet.Save();

                lPet = petshop.PetFactory.CreatePet();
                lPet.Source = "England";
                lPet.Type = "Dog [Labrador Retriever]";
                lPet.Save();

				t.Commit();
			}
			catch(Exception e) {
				t.RollBack();
				Console.WriteLine(e);
			}

        }

        private void RetrievePets(){
			System.Console.WriteLine("Here are the pets...");
			petshop.Pet[] lPets = petshop.PetFactory.ListPetByQuery(null, null);
            int length = Math.Min(lPets.Length, ROW_COUNT);
			for (int i = 0; i < length; i++) {
                System.Console.WriteLine(lPets[i].ORMID + ". " + lPets[i].Type + "; Source: " + lPets[i].Source);
			}
			
			System.Console.WriteLine(length + " record(s) retrieved.");

        }
		
		[STAThread]
		public static void Main(string[] args) {
            MyPetShop lMyPetShop = new MyPetShop();
			try {
                lMyPetShop.CreatePets();
                lMyPetShop.RetrievePets();
                Console.ReadLine();
			}
			finally {
				petshop.PetShopPersistentManager.Instance().DisposePersistentManager();
			}
			
		}
		
	}
}

